package com.itheima.book.controller;

import com.itheima.book.pojo.dto.MessagePageQueryDTO;
import com.itheima.book.pojo.entity.Message;
import com.itheima.book.service.MessageService;
import com.itheima.book.utils.result.PageResult;
import com.itheima.book.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/messages")
@Api(tags = "消息中心相关接口")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 分页查询消息列表
     */
    @GetMapping("/list")
    @ApiOperation("分页查询消息列表")
    public Result<PageResult> getMessageList(MessagePageQueryDTO messagePageQueryDTO) {
        log.info("分页查询消息列表: {}", messagePageQueryDTO);
        PageResult pageResult = messageService.getMessageList(messagePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 发送消息
     */
    @PostMapping("/send")
    @ApiOperation("发送消息")
    public Result sendMessage(@RequestBody Message message) {
        log.info("发送消息: {}", message);
        messageService.sendMessage(message);
        return Result.success();
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/read/{id}")
    @ApiOperation("标记消息为已读")
    public Result markAsRead(@PathVariable Long id) {
        log.info("标记消息为已读: {}", id);
        messageService.markAsRead(id);
        return Result.success();
    }

    /**
     * 获取未读消息数量
     */
    @GetMapping("/unread/count")
    @ApiOperation("获取未读消息数量")
    public Result<Integer> getUnreadCount() {
        Integer count = messageService.getUnreadCount();
        return Result.success(count);
    }

    /**
     * 获取联系人列表
     */
    @GetMapping("/contacts")
    @ApiOperation("获取联系人列表")
    public Result<List<Map<String, Object>>> getContacts() {
        log.info("获取联系人列表");
        List<Map<String, Object>> contacts = messageService.getContacts();
        return Result.success(contacts);
    }
} 